package com.test.sdk.common.util;

import java.security.MessageDigest;
import java.util.*;

/**
 * @author 徒有琴
 */
public class SignUtil {
    public static String getSignStr(Map<String, String[]> params) {
        StringBuilder builder = new StringBuilder();
        Set<String> keys = params.keySet();
        List<String> names = new ArrayList<>(keys);
        Collections.sort(names);//排序，默认升序
        for (String name : names) {
            if ("sign".equals(name)) {
                continue;
            }
            builder.append(name).append("=");
            String[] values = params.get(name);
            if (values != null && values.length > 0) {
                builder.append(values[0]);
            }
            builder.append("&");
        }
        return builder.toString();
    }

    public static String md5(String str) {
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(str.getBytes("UTF8"));
            byte s[] = m.digest();
            String result = "";
            for (int i = 0; i < s.length; i++) {
                result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
